﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Pg.BioMedics.SDR.Web.Controls
{
    /// <summary>
    /// This control extends rich text box control 
    /// with functionality delivered by RichText editor 
    /// from YUI (Yahoo User Interface). This control is intended to be used
    /// witin UpdatePanels or other Ajax controls
    /// </summary>
    public class RichTextBox : WebControl
    {
        #region Constants

        private const string DefaulyYUIBase = "~/Scripts/yui/";

        private const string YUILoaderScript = "build/yuiloader/yuiloader-beta-min.js";
        private const string YUIElementScript = "build/element/element-beta-min.js";
        private const string YUIContainerScript = "build/container/container_core-min.js";
        private const string YUISimpleEditorScript = "build/editor/simpleeditor-beta-min.js";
        private const string RichTextBoxScript = "Pg.BioMedics.SDR.Web.Controls.RichTextBox.RichTextBoxScripts.js";

        #endregion

        #region Fields

        private string yuiBase = DefaulyYUIBase;

        private ScriptManagerProxy scriptManagerProxy;
        private TextBox textBox;

        #endregion

        #region Properties

        /// <summary>
        /// Gets / sets base location of YUI library in this web application
        /// </summary>
        public string YUIBase
        {
            get
            {
                return this.yuiBase;
            }

            set
            {
                this.yuiBase = value;
            }
        }

        #endregion

        #region Overriden methods

        protected override void OnInit(EventArgs e)
        {       
        }

        protected override void CreateChildControls()
        {
            // add script manager proxy
            this.scriptManagerProxy = new ScriptManagerProxy();
            Controls.Add(this.scriptManagerProxy);

            this.scriptManagerProxy.Scripts.Add(new ScriptReference(this.yuiBase + YUILoaderScript));
            this.scriptManagerProxy.Scripts.Add(new ScriptReference(this.yuiBase + YUIElementScript));
            this.scriptManagerProxy.Scripts.Add(new ScriptReference(this.yuiBase + YUIContainerScript));
            this.scriptManagerProxy.Scripts.Add(new ScriptReference(this.yuiBase + YUISimpleEditorScript));
            
            // add text box
            this.textBox = new TextBox() { TextMode = TextBoxMode.MultiLine, 
                                           ID = Guid.NewGuid().ToString() };
            Controls.Add(this.textBox);     

            // register startup script
            ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, GetRichTextBoxScript(this.textBox.ID), true);
            
        }

        #endregion

        #region Support methods

        private string GetRichTextBoxScript(string textBoxID)
        {
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(RichTextBoxScript))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string scriptTemplate = reader.ReadToEnd();
                    return scriptTemplate.Replace("{textBoxID}", textBoxID);
                }
            }
        }

        #endregion
    }
}
